package ShoppingCartJFrame;

import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.*;

import javax.swing.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import client.product.Tickets;
import shoppingCart.*;

public class ItemPickerUI extends JFrame implements Observer{
	
	ObjectList items;
	ShoppingCart cart;
	FrameRegistry registry;
	Tickets ticket = new Tickets();
	
	private JButton describeItem = new JButton("Describe Item");
	private JButton addItem = new JButton("Add Item");
	
	public ItemPickerUI(StringReader reader)
	{
		// Watch the applet registry to see when the Shopping Cart applet
		// becomes active
				registry = FrameRegistry.instance();
				registry.addObserver(this);

				items = new ObjectList();

		// Get the URL of the list of items that are for sale
				
					//fetchItems();
				

		// Put the items in the center of the screen 
				setLayout(new BorderLayout());
				add("Center", items);

				checkForShoppingCart();

		// Add this applet to the registry
				registry.addFrame("Item Picker", this);
				
				// Adds a list of products available (tickets) in a form of
				// StringReader and starts the frame
				
				fetchItems(reader);
				setSize(460, 500);
				setVisible(true);
	}
	
	
	public void checkForShoppingCart()
	{
// See if the shopping cart has been loaded yet
		 JFrame frame = registry.findFrame("Shopping Cart");
		if (frame == null) return;

		ShoppingCartUI cartFrame = (ShoppingCartUI)
			frame;

// Get the shopping cart used by the shopping cart frame
		cart = cartFrame.getShoppingCart();

// Create the panel for adding items
		Panel southPanel = new Panel();

// Set up some command buttons for adding and describing items
		
		southPanel.add(describeItem);
				/*new CommandButton("Describe Item",
			new ItemPickerDescribe(this)));*/
		describeItem.addActionListener(new ActionListener (){
			public void actionPerformed(ActionEvent e){
				doDescribe();
			}
			});
		
		
		southPanel.add(addItem);
		addItem.addActionListener(new ActionListener (){
			public void actionPerformed(ActionEvent e){
				doAdd();
			}
			});
		/*	new CommandButton("Add Item",
			new ItemPickerAdd(this)));*/

		add("South", southPanel);
	}
	
	
	public void update(Observable obs, Object ob)
	{
		if (cart != null) return;

		checkForShoppingCart();
	}
	
	// When someone presses the "Add Item" button, the doAdd method
	// is called.
		public void doAdd()
		{
	// Find out what object was selected
			Object ob = items.getSelectedObject();

			if (ob == null) return;

	// Add the item to the cart
			cart.addItem(((ShoppingCartItem)ob).copy());
		}

	// When someone presses "Describe Item", the doDescribe method
	// is called.
		public void doDescribe()
		{

	// Find out which object was selected
			Object ob = items.getSelectedObject();

			if (ob == null) return;

			ShoppingCartItem item = (ShoppingCartItem) ob;

	// MARCOS - TO BE DONE		
	// If it has a description URL, open it up in another frame
			if (item.itemDesc != null) {
				/*getAppletContext().showDocument(
					item.itemDesc, "descframe");*/
				System.out.println(item.itemDesc);
			}
		}
		
		// parseItem extracts an item name, cost, and URL from a string. The
		// items should be separated by |'s.
			public void parseItem(Tickets ticket)
			{
				
				for (int i = 0; i <ticket.getTicket().toArray().length; i++){
				int id = ticket.getTicket().get(i).getTicketID();
				String name = ticket.getTicket().get(i).getTkName();
				String description = ticket.getTicket().get(i).getTkDescription();
				BigDecimal cost = ticket.getTicket().get(i).getTkPrice();
				int qty = ticket.getTicket().get(i).getTkAmount();
				
				items.addObject(name,
						new ShoppingCartItem(id, name, description, cost, qty));
				
				}
						
				/*		for (int i = 0; i <ticket.getTicket().toArray().length; i++){
							System.out.println("Ticket" + (i+1) +  " " +ticket.getTicket().get(i).getTkName()+
									" " + ticket.getTicket().get(i).getTkDescription() + "----" + 
									ticket.getTicket().get(i).getTkPrice());
							parseItem(ticket);

						}*/

			}

		// fetchItems gets a list of available items from the web server and
		// uses parseItem to parse the individual items. If a line begins with
		// the # character, it is ignored (# is typically a comment character).

			public void fetchItems(StringReader reader)
			{
				//StringReader reader = new StringReader(client.getProdInfo().toString());
				
					  JAXBContext context;
					try {
						context = JAXBContext.newInstance(Tickets.class);
						System.out.println("Output from the XML");
						Unmarshaller um = context.createUnmarshaller();
						ticket = (Tickets) um.unmarshal(reader);
						
					} catch (JAXBException e) {
						System.out.println("ItemPickerApplet at the method fetchItems SHHHEEEAAAAT");
					}
					parseItem(ticket);
					
					/*	for (int i = 0; i <ticket.getTicket().toArray().length; i++){
							System.out.println("Ticket" + (i+1) +  " " +ticket.getTicket().get(i).getTkName()+
									" " + ticket.getTicket().get(i).getTkDescription() + "----" + 
									ticket.getTicket().get(i).getTkPrice());
							parseItem(ticket);
						}*/
						
			}


	
		
}
